﻿using Community.CsharpSqlite.WP.SqliteDataProvider.Base;
using Community.CsharpSqlite.WP.SqliteDataProvider.Events;

namespace Community.CsharpSqlite.WP.SqliteDataProvider
{
    public class SqliteDataAdapter : DataAdapter
    {
        public delegate void SqliteRowUpdatingEventHandler(object sender, SqliteRowUpdatingEventArgs e);
        public delegate void SqliteRowUpdatedEventHandler(object sender, SqliteRowUpdatedEventArgs e);

        public event SqliteRowUpdatingEventHandler RowUpdating;
        public event SqliteRowUpdatedEventHandler RowUpdated;

        private SqliteCommand _selectCommand;
        private SqliteCommand _insertCommand;
        private SqliteCommand _updateCommand;
        private SqliteCommand _deleteCommand;
         
        static private readonly object EventRowUpdated = new object();
        static private readonly object EventRowUpdating = new object();

        public SqliteDataAdapter()
        {
        }

        /// <summary>
        /// Gets or sets the select command.
        /// </summary>
        /// <value>The select command.</value>
        public SqliteCommand SelectCommand
        {
            get { return _selectCommand; }
            set { _selectCommand = value; }
        }

        /// <summary>
        /// Gets or sets the insert command.
        /// </summary>
        /// <value>The insert command.</value>
        public SqliteCommand InsertCommand
        {
            get { return _insertCommand; }
            set { _insertCommand = value; }
        }

        /// <summary>
        /// Gets or sets the update command.
        /// </summary>
        /// <value>The update command.</value>
        public SqliteCommand UpdateCommand
        {
            get { return _updateCommand; }
            set { _updateCommand = value; }
        }

        /// <summary>
        /// Gets or sets the delete command.
        /// </summary>
        /// <value>The delete command.</value>
        public SqliteCommand DeleteCommand
        {
            get { return _deleteCommand; }
            set { _deleteCommand = value; }
        }


        override protected RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command)//, StatementType statementType, DataTableMapping tableMapping)
        {
            return new SqliteRowUpdatedEventArgs(dataRow, command); //, statementType, tableMapping);
        }

        override protected RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command)//, StatementType statementType, DataTableMapping tableMapping)
        {
            return new SqliteRowUpdatingEventArgs(dataRow, command); //, statementType, tableMapping);
        }

        override protected void OnRowUpdating(RowUpdatingEventArgs value)
        {
            if (RowUpdating != null)
                RowUpdating(this, (SqliteRowUpdatingEventArgs) value);
        }

        override protected void OnRowUpdated(RowUpdatedEventArgs value)
        {
            if (RowUpdated != null)
                RowUpdated(this, (SqliteRowUpdatedEventArgs) value);
        }
    }


    public class SqliteRowUpdatingEventArgs : RowUpdatingEventArgs
    {
        public SqliteRowUpdatingEventArgs(DataRow row, IDbCommand command )//, StatementType statementType, DataTableMapping tableMapping)
            : base(row, command) //, statementType, tableMapping)
        {
        }

        // Hide the inherited implementation of the command property.
        new public SqliteCommand Command
        {
            get { return (SqliteCommand)base.Command; }
            set { base.Command = value; }
        }
    }

    public class SqliteRowUpdatedEventArgs : RowUpdatedEventArgs
    {
        public SqliteRowUpdatedEventArgs(DataRow row, IDbCommand command) //, StatementType statementType, DataTableMapping tableMapping)
            : base(row, command)//, statementType, tableMapping)
        {
        }

        // Hide the inherited implementation of the command property.
        new public SqliteCommand Command
        {
            get { return (SqliteCommand)base.Command; }
        }
    }
}
